﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Topro.WCS
{
    using Topro.Application;
    using Topro.Entity.Query;
    using Topro.Entity.Response;
    using Topro.Print;

    /// <summary>
    /// 容器
    /// </summary>
    public partial class frmContainer : Form
    {
        public frmContainer()
        {
            InitializeComponent();

            #region 出入口样式
            //样式
            dgContainer.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;//列宽不自动调整,手工添加列
            //dgGateWay.RowHeadersWidth = 12;//行标题宽度固定12
            dgContainer.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;//不能用鼠标调整列标头宽度
            //dgGateWay.AlternatingRowsDefaultCellStyle.BackColor = Color.LemonChiffon;//奇数行背景色
            dgContainer.BackgroundColor = Color.White;//控件背景色
            dgContainer.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//列标题居中显示
            dgContainer.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//单元格内容居中显示
            //行为
            dgContainer.AutoGenerateColumns = false;//不自动创建列
            dgContainer.AllowUserToAddRows = false;//不启用添加
            dgContainer.ReadOnly = true;//不启用编辑
            dgContainer.AllowUserToDeleteRows = false;//不启用删除
            dgContainer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//单击单元格选中整行
            dgContainer.MultiSelect = false;//不能多选
            dgContainer.AllowUserToResizeRows = false;//禁止用户改变DataGridView1の所有行的行高
            dgContainer.RowTemplate.Height = 20;//设置行高
            //dgGateWay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;//自动填充
            dgContainer.RowHeadersVisible = false;
            #endregion
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            CommandQuery();
        }

        /// <summary>
        /// 指令查询
        /// </summary>
        public void CommandQuery()
        {
            //参数
            var query = new ContainerListQuery();
            //获取选中的值
            //var status = cbbCommandStatus.SelectedValue;
            //if (status != null)
            //{
            //    query.Status = (int)cbbCommandStatus.SelectedValue;
            //}
            //query.Status = 40;
            //查询
            //var list = ASRSJOBApp.Instance.GetList(query);
            //绑定数据
            //this.dgContainer.DataSource = new BindingList<ContainerResponse>(list);
        }

        /// <summary>
        /// 指令右健单元格菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgContainer_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (dgContainer.Rows[e.RowIndex].Selected == false)
                    {
                        dgContainer.ClearSelection();
                        dgContainer.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dgContainer.SelectedRows.Count == 1)
                    {
                        dgContainer.CurrentCell = dgContainer.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    msCommand.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiPrint_Click(object sender, EventArgs e)
        {
            int count = dgContainer.SelectedRows.Count;
            if (count <= 0)
                MessageBox.Show("请选择要完成的行.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //选中行
            var row = dgContainer.SelectedRows;
            if (row == null)
                MessageBox.Show("请选择要完成的行.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //当前选中的索引
            var index = dgContainer.CurrentRow.Index;
            //当前选择中的行第一个单元格
            var id = (int)dgContainer.Rows[index].Cells[0].Value;
            if (id <= 0)
                MessageBox.Show("请选择要完成的行.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //容器编码
            var qrCode = dgContainer.Rows[index].Cells[1].Value.ToString();
            if (string.IsNullOrEmpty(qrCode))
            {
                MessageBox.Show("容器编码获取失败.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            PrintImpl.QRCode(qrCode);

        }
    }
}
